package com.shoeHouse.orderEvaluate.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.shoeHouse.orderEvaluate.dao.OrderEvaluateMapper;
import com.shoeHouse.orderEvaluate.model.OrderEvaluate;
import com.shoeHouse.orderEvaluate.model.OrderEvaluateExample;
import com.shoeHouse.orderEvaluate.model.OrderEvaluateExample.Criteria;
import com.shoeHouse.util.PageHelper;

@Service
public class OrderEvaluateServiceImpl implements OrderEvaluateService {
	@Autowired
	private OrderEvaluateMapper orderEvaluateMapper;

	@Override
	public void save(OrderEvaluate o) {
		// TODO Auto-generated method stub
		orderEvaluateMapper.insert(o);
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(OrderEvaluate o) {
		// TODO Auto-generated method stub
		
		OrderEvaluateExample example=new OrderEvaluateExample();
		Criteria criteria=example.createCriteria();
		criteria.andIdEqualTo(o.getId());
		orderEvaluateMapper.updateByExampleSelective(o, example);
		
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		OrderEvaluateExample example=new OrderEvaluateExample();
	
		Criteria criteria=example.createCriteria();
		criteria.andOrderEvaluateStatusEqualTo("0");
		int count=orderEvaluateMapper.countByExample(example);
		return count;
	}

	@Override
	public List<OrderEvaluate> getAll(HttpServletRequest request,OrderEvaluate orderEvaluate, PageHelper pageHelper) {
		// TODO Auto-generated method stub
		String limit="limit "+(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize()+","+pageHelper.getPageSize();
		OrderEvaluateExample example=new OrderEvaluateExample();
		example.setOrderByClause("gmt_modified DESC "+limit);
		Criteria criteria=example.createCriteria();
		criteria.andOrderEvaluateStatusEqualTo("0");
	
		if(null!=orderEvaluate.getCommodifyId()&&!orderEvaluate.getCommodifyId().equals("")){
			criteria.andCommodifyIdEqualTo(orderEvaluate.getCommodifyId());
			
		}
		String endTime=request.getParameter("endTime");
		String beginTime=request.getParameter("beginTime");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		if(beginTime!=null){
			if(endTime!=null){
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),sdf.parse(endTime));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),new Date());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		if(null!=orderEvaluate.getSellerId()&&!orderEvaluate.getSellerId().equals("")){
			criteria.andSellerIdEqualTo(orderEvaluate.getSellerId());
		}
		List<OrderEvaluate> list=orderEvaluateMapper.selectByExample(example);
		
		return list;
	}

	@Override
	public OrderEvaluate getSingle(OrderEvaluate orderEvaluate) {
		// TODO Auto-generated method stub
		OrderEvaluate evaluate=orderEvaluateMapper.selectByPrimaryKey(orderEvaluate.getId());
		return evaluate;
	}
    
}
